/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.member.service.impl;

import iaas.enums.StatusEnum;
import iaas.enums.YesOrNoEnum;
import iaas.persistence.DynamicSpecifications;
import iaas.persistence.SearchFilter;
import iaas.persistence.SearchFilter.Operator;
import iaas.service.impl.BaseServiceImpl;
import iaas.utils.ResultObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.wealth.member.entity.Address;
import com.wealth.member.repository.AddressDao;
import com.wealth.member.service.IAddressService;
import com.wealth.order.entity.Order;

/**
 * @author chan
 */
 @Component
public class AddressServiceImpl extends BaseServiceImpl<Address> implements IAddressService{

	@Autowired
	private AddressDao addressDao;


	@Override
	public List<Address> getByCorpIdAndStatus(Long corpid,Integer status) throws Exception {
		return addressDao.findByCorpIdAndStatus(corpid, status);
	}

	/**  ---------------- 微信端  ---------------- */
	/** 查询 会员+状态不等于 */
	@Override
	public List<Address> getAddressByMemberIdAndStatusNot(Long memberid, Integer status) throws Exception {
		return addressDao.findByMemberIdAndStatusNotOrderByIsDefaultDesc(memberid, status);
	}

	/** 查询 会员+状态+默认  */
	@Override
	public Address getAddressDefault(Long memberid) {
		List<Address> addresses = addressDao.findByMemberIdAndStatusAndIsDefault(memberid,StatusEnum.NORMAL.getValue(),YesOrNoEnum.YES.getValue());
		for (Address address : addresses) {
			if(address.getIsDefault()==1){
				return address;
			}
		}
		return null;
	}

	@Override
	public List<Address> findByMemberId(Long memberid) {
		
		return addressDao.findByMemberId(memberid);
	}

	/**
	 *  分页查询
	 */
	public Page<Address> getAddress(Long memberid, Map<String, Object> searchParams, int pageNumber, int pageSize,
			String sortType,String sortDirection) {
		
		//创建分页请求.
		Direction direction = sortDirection.equals("desc")?Direction.DESC:Direction.ASC;
		Sort sort = new Sort(direction, sortType);
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sort);

		//创建动态查询条件组合.
		Collection<SearchFilter> filters = SearchFilter.parse(searchParams);
		filters.add(new SearchFilter("member.id", Operator.EQ, memberid));
		filters.add(new SearchFilter("status", Operator.NE, StatusEnum.DELETE.getValue()));
		Specification<Address> spec = DynamicSpecifications.bySearchFilter(filters, Address.class);
		return addressDao.findAll(spec, pageRequest);
	}

	/**
	 * 查询收货地址 全部
	 */
	public List<Address> getAllAddress() {
		return (List<Address>) addressDao.findAll();
	}
	
	/**
	 * 查询收货地址 单一
	 */
	public Address getAddress(Long id) {
		return addressDao.findOne(id);
	}
	
	/**
	 * 创建收货地址
	 */
	@Transactional(readOnly=false)
	public void saveAddress(Address address) {
		addressDao.save(address);
	}

	/**
	 * 编辑收货地址
	 * @FIXME
	 */
	@Transactional(readOnly=false)
	public void updateAddress(Address address){
		//获得默认的时候地址
		//这里发现,在查询前会将传入的address更新?不知道为什么
		//Address temp = getAddressDefault(ShiroMember.getInstance().getMember().getId());
		addressDao.save(address);
	}

	/**
	 * 删除收货地址
	 */
	@Transactional(readOnly=false)
	public void deleteAddress(Long id) {
		Address address = addressDao.findOne(id);
		if(address.getIsDefault()!=1){
			address.setStatus(StatusEnum.DELETE.getValue());
			addressDao.save(address);
		}
	}

	@Override
	public ResultObject getAddressPage(Long memberid, Integer pageSize, Integer pageNumber) throws Exception {
		//创建分页请求.
		Direction direction =Direction.DESC;
		Sort sort = new Sort(direction, "id");
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sort);

		//创建动态查询条件组合.
		Collection<SearchFilter> filters = new ArrayList<SearchFilter>();
		filters.add(new SearchFilter("member.id", Operator.EQ, memberid));
		filters.add(new SearchFilter("status", Operator.NE, StatusEnum.DELETE.getValue()));
		Specification<Address> spec = DynamicSpecifications.bySearchFilter(filters, Address.class);
		Page<Address> page =addressDao.findAll(spec, pageRequest);
		List<Address> addressList = page.getContent();
		Long count = page.getTotalElements();
		return new ResultObject("000", String.valueOf(count), addressList);
	}

}


